/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-06-13 14:51:56
 * @LastEditors: TT
 * @LastEditTime: 2023-02-28 10:12:09
 */

import 'package:flutter/material.dart';
import 'package:ttcomment/ttcomment.dart';
import 'package:get/get.dart';

/// 跳转到指定界面
currentToPage(
  String name, {
  Map<String, String>? parameters,
  dynamic arguments,
  Function(Map info)? onChange,
  bool preventDuplicates = true,
}) {
  if (name == "") {
    return;
  }

  return Get.toNamed(
    name,
    parameters: parameters,
    arguments: arguments,
    preventDuplicates: preventDuplicates,
  );
}

/// 重定向入口页
currentTo({
  required String name,
  Map<String, String>? parameters,
  dynamic arguments,
}) {
  Get.offAllNamed(
    name,
    parameters: parameters,
    arguments: arguments,
  );
}

/// 跳转到指定页面,并关闭当前页面
currentOffAndToName({
  required String name,
  Map<String, String>? parameters,
  dynamic arguments,
}) {
  Get.offAndToNamed(
    name,
    arguments: arguments,
    parameters: parameters,
  );
}

currentOffName({
  required String name,
  Map<String, String>? parameters,
  dynamic arguments,
}) {
  Get.offNamed(
    name,
    arguments: arguments,
    parameters: parameters,
  );
}

// ================= 返回界面 ============== //

// 返回到指定界面
// `Get.until((route) => Get.currentRoute == '/home')`
currentUntil({required String name}) {
  Get.until((route) => Get.currentRoute == name);
}

// 返回到指定页面，并创建指定页面
// backPageName 需要返回的界面
// toPageName 需要创建的界面
currentOffNamedUntil({
  required String backPageName,
  required String toPageName,
  Map<String, String>? parameters,
  dynamic arguments,
}) {
  Get.offNamedUntil(
    toPageName,
    (route) => Get.currentRoute == backPageName,
    arguments: arguments,
    parameters: parameters,
  );
}

// 替换当前页
currentOff({required String page}) {
  Get.off(page);
}

/// 返回上一个界面
void currentGoback(BuildContext context, {Map? info}) {
  CommentTools.keydissmiss(context);
  Get.back(result: info);
}

//// 创建新页面，并关闭其他页面
currentGetofName(
  String name, {
  dynamic arguments,
  Map<String, String>? parameters,
}) {
  Get.offAllNamed(
    name,
    arguments: arguments,
    parameters: parameters,
  );
}

/// 获取界面传值 parameters
Map<String, String?> currentGetParams() {
  return Get.parameters;
}

/// 获取界面传值 arguments
dynamic currentGetArguments() {
  return Get.arguments;
}
